package com.example.product.service.impl;

import com.example.product.dao.AttrAttrgroupRelationDao;
import com.example.product.dao.AttrDao;
import com.example.product.entity.AttrAttrgroupRelationEntity;
import com.example.product.entity.AttrEntity;
import com.example.product.vo.AttrGroupWithAttrRespVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.product.dao.AttrGroupDao;
import com.example.product.entity.AttrGroupEntity;
import com.example.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Slf4j
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        String key = (String)params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if (catId != 0) {
            queryWrapper = queryWrapper.eq("catelog_id",catId);
        }


        if (!StringUtils.isEmpty(key)) {
            if (catId == 0) {
                queryWrapper = queryWrapper.eq("attr_group_id",key).or().like("attr_group_name",key);
            }else {
                queryWrapper = queryWrapper.and(qw -> {
                    qw.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> queryAttrGroupRelationAttrs(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> attrgroupRelationEntities = attrAttrgroupRelationDao.selectList(
                new QueryWrapper<AttrAttrgroupRelationEntity>()
                        .eq("attr_group_id", attrGroupId));

        List<AttrEntity> attrEntities = null;
        if (attrgroupRelationEntities != null) {
            attrEntities = attrgroupRelationEntities.stream().map(relation -> {
                if (relation != null) {
                    AttrEntity attrEntity = attrDao.selectById(relation.getAttrId());
                    return attrEntity;
                }
                return null;
            }).collect(Collectors.toList());
        }
        return attrEntities;
    }

    @Override
    public void removeAttrGroupRelationAttr(AttrAttrgroupRelationEntity[] entities) {
        List<AttrAttrgroupRelationEntity> relations = Arrays.asList(entities);
        if (relations.size() > 0) {
            attrAttrgroupRelationDao.deleteAttrGroupRelationAttr(relations);
        }
    }

    @Override
    public List<AttrGroupWithAttrRespVo> getAttrGroupWithAttrs(Long catelogId) {
        // 1. 根据分类id查询属性组
        if (catelogId == null) {
            return null;
        }
        List<AttrGroupEntity> groupEntities = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        if (groupEntities == null || groupEntities.size() == 0) {
            return null;
        }

        List<AttrGroupWithAttrRespVo> attrGroupWithAttrRespVos = groupEntities.stream().map(item -> {
            // 拷贝对象
            AttrGroupWithAttrRespVo vo = new AttrGroupWithAttrRespVo();
            BeanUtils.copyProperties(item, vo);
            // 查询分组关联的基本属性
            List<AttrAttrgroupRelationEntity> relations = attrAttrgroupRelationDao.selectList(
                    new QueryWrapper<AttrAttrgroupRelationEntity>()
                            .eq("attr_group_id", item.getAttrGroupId()));
            if (relations != null && relations.size() > 0) {
                List<Long> attrIds = relations.stream().map(relation -> {
                    return relation.getAttrId();
                }).collect(Collectors.toList());
                List<AttrEntity> attrs = attrDao.selectList(new QueryWrapper<AttrEntity>().in("attr_id", attrIds));
                vo.setAttrs(attrs);
            }
            return vo;
        }).collect(Collectors.toList());
        return attrGroupWithAttrRespVos;
    }

}